$('.datatable-basic').DataTable({
	 "ajax" : {
		"url" : contextpath + "customer/dlist",
		"data" : function(d) {
            var query = $("#query").serializeObject();
            query["size"] = d.length;
            query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.totalElements;
            json.recordsFiltered = json.totalElements;
            json.data = json.content;
 
            return JSON.stringify( json );
        }
	 },
	 "columns": [
	             {
	     			render : function(data,type, row, meta) {
	                     return '<a href="'+contextpath+'customer/detail/'+row["id"]+'">'+row["shopName"]+'</a>';
	                 }
	     		 },
         { "data": "customerCount" },
         { "data": "phoneNo" },
         { "data": "allRebate" },
         { "data": "allWithdrawRebate" },
         { "data": "leftRebate" },
         {
            render : function(data,type, row, meta) {
            	return $("#btnGroup").html();
            }
        }
     ],
     "drawCallback": function( settings ) {
		 $(".btn-edit").click(function(){
			/*$("#phoneNo").attr("readonly","readonly");
			$("#edit").validate().resetForm();
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
		    $("#edit").fill(row);
		    $(".select").trigger("change");
		    
		    $("#type").val(row.type);
		    $("#auditStatus").val(row.auditStatus);
		    
		    $(":input[name='id']").val(row.id);
		    $("#registerTime").val(row.registerTime);
		    $("#cooperationStartTime").val(row.cooperationStartTime);
		    $("#cooperationEndTime").val(row.cooperationEndTime);*/
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			window.location.href = contextpath + "customer/distributorEdit?id=" + row.id;
		 });
		 
		 $(".btn-audit").click(function(){
			 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			 $("#id").val(row.id);
	     });
		 
		 $(".btn-del").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			swal({
				title : "修改状态",
				text : "确定要删除渠道商： "+row.name+"?(将删除该渠道商与原客户关系，推荐渠道商关系)",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "确定",
				cancelButtonText: "取消",
				closeOnConfirm : false
			},function() {
				$.get(contextpath + "customer/delete/" + row.id, function(d){
					$('.datatable-basic').DataTable().draw();
					swal("删除渠道商成功","", "success");
				})
			});
			
		 });
      }
});

$(function(){
	$(".btn-add").click(function(){
		/*$("#edit").validate().resetForm();
		$("#phoneNo").removeAttr("readonly");
		$("#edit")[0].reset();
		$(":input[name='id']").val();*/
		window.location.href = contextpath + "customer/distributorEdit";
	});
	
	$("#submitEdit").click(function(){
		if($("#edit").valid()){
			$.post(contextpath + "customer/save", $("#edit").serialize(), function(data){
					$(":input[name='status']").removeAttr("disabled");
					$('.datatable-basic').DataTable().draw();
					$('.close').click();
					swal("编辑渠道商成功","", "success");
			})
		}
	});
	
	$("#submitAudit").click(function(){
		if($("#audit").valid()){
			$.post(contextpath + "customer/audit", $("#audit").serialize(), function(){
		        /*$(":input[name='status']").removeAttr("disabled");*/
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("审核渠道商成功","", "success");
			})
		}
	});
	
	$("#submit").click(function(){
		$('.datatable-basic').DataTable().draw();
	});

	$('select').select2({
	    minimumResultsForSearch: Infinity
	});	
	
	$('#cooperationStartTime').daterangepicker(
			  { 
				  "singleDatePicker": true,
				  "parentEl":$("#edit_dialog"),
			    locale:{
			    	format: 'YYYY-MM-DD',
		            applyLabel: '确认',
		            cancelLabel: '取消',
		            fromLabel: '从',
		            toLabel: '到',
		            customRangeLabel: '选择时间',
		            daysOfWeek:["日","一","二","三","四","五","六"],
		            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		        }
			  }
			);
	 $('#cooperationStartTime').val("");
	 
	 $('#cooperationEndTime').daterangepicker(
			  { 
				  "singleDatePicker": true,
				  "parentEl":$("#edit_dialog"),
			    locale:{
			    	format: 'YYYY-MM-DD',
		            applyLabel: '确认',
		            cancelLabel: '取消',
		            fromLabel: '从',
		            toLabel: '到',
		            customRangeLabel: '选择时间',
		            daysOfWeek:["日","一","二","三","四","五","六"],
		            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		        }
			  }
			);
	 $('#cooperationEndTime').val("");
	 
	 $('#registerTime').daterangepicker(
			  { 
				  "singleDatePicker": true,
				  "parentEl":$("#edit_dialog"),
			    locale:{
			    	format: 'YYYY-MM-DD',
		            applyLabel: '确认',
		            cancelLabel: '取消',
		            fromLabel: '从',
		            toLabel: '到',
		            customRangeLabel: '选择时间',
		            daysOfWeek:["日","一","二","三","四","五","六"],
		            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		        }
			  }
			);
	 $('#registerTime').val("");
	
})
